def longest_substring_with_k_distinct(string, k):
  substr = ''
  times = 0
  maxlength = 0
  for end in range(len(string)):
    if(string[end] not in substr):
      times += 1
    substr += string[end]
    while(times>k):
      if(substr.count(substr[0])==1):
        times-=1
      substr = substr[1:]
    maxlength = max(maxlength,len(substr))
  return maxlength

def main():
  print("Length of the longest substring: " + str(longest_substring_with_k_distinct("araaci", 2)))
  print("Length of the longest substring: " + str(longest_substring_with_k_distinct("araaci", 1)))
  print("Length of the longest substring: " + str(longest_substring_with_k_distinct("cbbebi", 3)))


main()